package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.TradeAdapter;
import com.hemaapp.yjnh.bean.TradeRecord;
import com.hemaapp.yjnh.bean.User;

public class TradeListActivity extends BaseActivity {

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	private RefreshLoadmoreLayout layoutRefresh;
	private XtomListView listview;//推荐商品
	private LinearLayout ll_return_top;
	
	private ArrayList<TradeRecord> records = new ArrayList<TradeRecord>();
	private TradeAdapter adapter;
	private int page = 0;
	private String type = "1";//消费类型:1：全部;2：消费;3：赚取;4：退款所得;5：提现；6:充值
	private User user;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_cmn_refresh_list_title);
		super.onCreate(savedInstanceState);
		user = getApplicationContext().getUser();
		getTradeRecords();
		listview.setDividerHeight(0);
	}
	
	private void getTradeRecords() {
		if (user != null) {
			getNetWorker().getTradeList(user.getToken(), type, page+"");
		}
	}
	
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case TRADE_LIST:
			showProgressDialog("请稍候");
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case TRADE_LIST:
			cancelProgressDialog();
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case TRADE_LIST:
			HemaPageArrayResult<TradeRecord> bResult = (HemaPageArrayResult<TradeRecord>) baseResult;
			if (bResult == null || bResult.getObjects() == null)
				return;
			if (page == 0) {// 刷新
				layoutRefresh.refreshSuccess();
				layoutRefresh.setLoadmoreable(true);
				records.clear();
				this.records.addAll(bResult.getObjects());
			}else {
				layoutRefresh.loadmoreSuccess();
				if (bResult.getObjects().size()>0) {
					this.records.addAll(bResult.getObjects());
				}else {
					layoutRefresh.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			refreshData(-1);
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case TRADE_LIST:
//			showProgress = true;
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
			}else {
				layoutRefresh.loadmoreFailed();
			}
			XtomToastUtil.showShortToast(mContext, "获取数据失败,"+baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case TRADE_LIST:
//			showProgress = true;
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
				refreshData(failedType);
			}else {
				layoutRefresh.loadmoreFailed();
				refreshData(failedType);
			}
            XtomToastUtil.showShortToast(mContext, "操作失败，请稍后再试");
			break;
		default:
			break;
		}
	}
	
	private void refreshData(int failedType) {
		if (adapter == null) {
			adapter = new TradeAdapter(mContext, records);
			adapter.setEmptyString("没有交易记录");
			listview.setAdapter(adapter);
		}else {
			adapter.setEmptyString("没有交易记录");
			adapter.notifyDataSetChanged();
		}
		adapter.setFailtype(failedType);
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		layoutRefresh = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listview = (XtomListView) findViewById(R.id.listview);
		ll_return_top = (LinearLayout) findViewById(R.id.ll_return_top);//回到顶部
	}

	@Override
	protected void setListener() {
		titleText.setText("交易记录");
		titleLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.GONE);

		layoutRefresh.setOnStartListener(new StartListener());
		ll_return_top.setVisibility(View.GONE);
	}

	private class StartListener implements OnStartListener {

		@Override
		public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
			page = 0;
			getTradeRecords();
		}

		@Override
		public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
			page++;
			getTradeRecords();
		}
	}
}
